package com.jeecg.struts.util;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.jeecg.springmvc.annotation.Controller;
import com.jeecg.springmvc.annotation.RequestMapping;
import com.jeecg.springmvc.bean.ControlForm;

public class ReadAnnotation {

	public static String property = System.getProperty("user.dir") + "\\src";
	public static String SRCSTR = "src\\";

	public static void main(String[] args) {
		new ReadAnnotation().readFile();
	}

	public static  String subReapleaPath(String filePathName) {
		filePathName = filePathName.substring(filePathName.indexOf(SRCSTR)
				+ SRCSTR.length());
		filePathName = filePathName.replaceAll("\\\\", ".").replaceAll(".java",
				"");
		return filePathName;
	}

	
	
	// 扫描所有类 找到对应的@control类
	public static void scanFile(Map<String,List<ControlForm>> annotationMap,List<ControlForm> methodList , String className)	
			throws InstantiationException, IllegalAccessException {
		
		if (className != null && !className.equals("")) {
			 try {
				    String classParams = null;
					Class<?> forName = Class.forName(className);
					// 判断当前class 是否包含注解
					if (!forName.isAnnotationPresent(Controller.class)) {
						return; 
					}
					
					if (forName.isAnnotationPresent(RequestMapping.class)) {
						System.out.println(className);
						Annotation[] annotations = forName.getAnnotations();
						for (Annotation annotation : annotations) {
							if (annotation.annotationType().getName().equals(
									RequestMapping.class.getName())) {
								RequestMapping newInstance = (RequestMapping)forName.getAnnotation(RequestMapping.class) ;
								classParams = newInstance.params();
								System.out.println("classUrl:" + classParams);
							}
						}
					}
					
					//得到当前类的所有方法
					Method[] methods = forName.getMethods();
					ControlForm form = new ControlForm();
					Map<String,Map<String,String>> methodParamNameMap = new HashMap<String, Map<String,String>>();
					for (int i = 0; i < methods.length; i++) {
						Method method = methods[i];
						if(method.isAnnotationPresent(RequestMapping.class)){
							Map <String,String> paramNameMap = new HashMap<String, String>();
							String methodName = method.getName();
							Class<?>[] parameterTypes = method.getParameterTypes();
							String[] methodParameterNamesByAsm4 = ParameterNameUtils.getMethodParameterNamesByAsm4(forName,method);
							for (int j = 0; j < parameterTypes.length; j++) {
								//参数名字
								String paramName = methodParameterNamesByAsm4[j];
								paramNameMap.put(paramName, parameterTypes[j].getName());	
							}
							methodParamNameMap.put(methodName, paramNameMap);
						}
					}
					
					form.setClassName(className);
					form.setMethodParamNameMap(methodParamNameMap);
					methodList.add(form);
				    if(classParams!=null && methodList!=null && methodList.size()>0){
				    	annotationMap.put(classParams, methodList);
				    }
					
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	/**
	 * 递归遍历src下的所有java
	 * @param filePath
	 * @return
	 */
	public static Map<String,List<ControlForm>> readFile() {
		Map<String,List<ControlForm>> annotationMap = new HashMap<String,List<ControlForm>>();
		List<ControlForm> methodList = new ArrayList<ControlForm>();
		Set set=PackageUtil.getClasses("com.jeecg.springmvc.controller");  
	        for (Iterator iter = set.iterator(); iter.hasNext();) {  
	            Class _class=(Class) iter.next();  
	            System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< "+_class.getName());  
	            try {
					scanFile(annotationMap,methodList, _class.getName());
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
	    }  
     Set<String> keySet = annotationMap.keySet();
		for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			System.out.println("key:"+key);
			List<ControlForm> list = annotationMap.get(key);
            for (ControlForm controlForm : list) {
				System.out.println("类名："+controlForm.getClassName());
				Map<String, Map<String, String>> methodParamNameMap = controlForm.getMethodParamNameMap();
				Set<String> keySet2 = methodParamNameMap.keySet();
				for (Iterator iterator2 = keySet2.iterator(); iterator2
						.hasNext();) {
					String methodName = (String) iterator2.next();
					Map<String, String> paramMap = methodParamNameMap.get(methodName);
					System.out.println("方法名："+methodName);
					Set<String> keySet3 = paramMap.keySet();
					for (Iterator iterator3 = keySet3.iterator(); iterator3
							.hasNext();) {
						String paramName = (String) iterator3.next();
						String paramType = paramMap.get(paramName);
						System.out.println("参数名："+paramName);
						System.out.println("参数类型："+paramType);
					}
				}
			}		
		}
		
		return annotationMap;
	}
	
	
	
	
	/**
	 * 递归遍历src下的所有java
	 * @param filePath
	 * @return
	 */
	/*public static Map<String,List<ControlForm>> readFile2(String filePath) {
		File f = new File(filePath);
		Map<String,List<ControlForm>> annotationMap = new HashMap<String,List<ControlForm>>();
		List<ControlForm> methodList = new ArrayList<ControlForm>();

		if (f.exists()) {
			File[] listFiles = f.listFiles();
			for (File file : listFiles) {
				if (file.isDirectory()) {
					readFile2(file.getPath());
				} else {
					String path = file.getPath();
					try {
						scanFile(annotationMap,methodList, subReapleaPath(path));
					} catch (InstantiationException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			}
		}
		
		Set<String> keySet = annotationMap.keySet();
		for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			System.out.println("key:"+key);
			List<ControlForm> list = annotationMap.get(key);
            for (ControlForm controlForm : list) {
				System.out.println("类名："+controlForm.getClassName());
				Map<String, Map<String, String>> methodParamNameMap = controlForm.getMethodParamNameMap();
				Set<String> keySet2 = methodParamNameMap.keySet();
				for (Iterator iterator2 = keySet2.iterator(); iterator2
						.hasNext();) {
					String methodName = (String) iterator2.next();
					Map<String, String> paramMap = methodParamNameMap.get(methodName);
					System.out.println("方法名："+methodName);
					Set<String> keySet3 = paramMap.keySet();
					for (Iterator iterator3 = keySet3.iterator(); iterator3
							.hasNext();) {
						String paramName = (String) iterator3.next();
						String paramType = paramMap.get(paramName);
						System.out.println("参数名："+paramName);
						System.out.println("参数类型："+paramType);
					}
				}
			}		
		}
		return annotationMap;
	}
*/
	

	
	
	
}
